﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using CommonLibrary.DataModel.Form;
using CommonLibrary.CustControls;
using CommonLibrary.DataModel.WorkFlow;
using System.IO;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using ToolHelper.DataGridViewExtern;
using System.ComponentModel.Design;
using System.Drawing;

namespace CommonLibrary.Common
{
    public class SerializeDesigner
    {
        #region 控件转化为xml格式
        //自定义命名变量(C1\C2\C3..)
        int numName = 1;
        XElement serializedItem = null;
        XElement serializedPageItems = null;
        XElement serializedItems = null;
        int maxHeight = 0;//记录每张表单中控件位置最下面的位置高度=控件的top+height;
        /// <summary>
        /// 序列化控件,返回自定义格式的xml体
        /// </summary>
        /// <param name="MdiContiner"></param>
        /// <returns></returns>
        public XElement SerializeControlToXml(TabControl MdiContiner, String formName)
        {
            serializedItems = new XElement("Form", new XAttribute("FormName", formName));
            for (int index = 0; index < MdiContiner.TabPages.Count; index++)
            {
                maxHeight = 0;//读取每张表单时初始化，在下面 Getcontrols 方法中遍历控件，并把最大高度赋值给 maxHeight                
                serializedPageItems = new XElement("TabPage" + (index + 1), new XAttribute("maxHeight", 0));
                Getcontrols(MdiContiner.TabPages[index]);
                serializedPageItems.Attribute("maxHeight").Value = maxHeight.ToString();
                serializedItems.Add(serializedPageItems);
            }
            return serializedItems;
        }

        private void Getcontrols(Control control)
        {
            foreach (Control item in control.Controls)
            {
                GetControlType(item);
                if (item.Controls.Count > 0)
                    Getcontrols(item);
            }
        }

        private void GetControlType(Control item)
        {

            StringBuilder value = null;

            XElement XElementText = new XElement("Text", item.Text);

            XElement XElementLeft = new XElement("Left", item.Left);
            XElement XElementTop = new XElement("Top", item.Top + 8);
            XElement XElementWidth = new XElement("Width", item.Width);
            XElement XElementHeight = new XElement("Height", item.Height);
            //获取判断并决定是否把当前控件的top+height 赋值给maxHeight   mlh
            string[] arr = { "BD_Label", "BD_TextBox", "BD_RichTextBox", "BD_Button", "BD_RadioButton", "BD_CheckBox", "BD_ComboBox", "BD_PictureBox", "BD_Horizon", "BD_Vertical", "BD_DataGridView", "BD_TreeView_Employee" };
            var res = (from x in arr
                       where x == item.GetType().Name
                       select x).FirstOrDefault();
            if (!string.IsNullOrEmpty(res))
                maxHeight = maxHeight > (item.Top + item.Height) ? maxHeight : (item.Top + item.Height);
            XElement XElementForeColor = new XElement("ForeColor", item.ForeColor);
            XElement XElementBackColor = new XElement("BackColor", item.BackColor);
            XElement XElementFontName = new XElement("FontName", item.Font.Name);
            XElement XElementFontSize = new XElement("FontSize", item.Font.Size + 3);
            //font-family: "宋体"; font-size: 12px; color: red;
            //XElement XElementBackColor = new XElement("BackColor", item.BackColor.ToKnownColor().ToString());

            serializedItem = new XElement("field", XElementText,
                XElementLeft, XElementTop, XElementWidth, XElementHeight,
                XElementForeColor, XElementBackColor, XElementFontName, XElementFontSize);

            string typename = item.GetType().Name;
            switch (item.GetType().Name)
            {
                //case "Form":
                //    serializedItem = new XElement("field", new XElement("Type", "Form"), new XElement("Name", (item as Form).Name));
                //    serializedPageItems.Add(serializedItem);
                //    return;
                case "BD_Label":
                    serializedItem.Add(new XElement("FontBold", (item as BD_Label).FontBold));
                    //  serializedItem.Add(new XElement("FontSize", (item as BD_Label).Font.Size + 3));
                    serializedItem.Add(new XElement("FontItalic", (item as BD_Label).FontItalic));
                    serializedItem.Add(new XElement("TextAlign", (item as BD_Label).TextAlign));
                    serializedItem.Add(new XElement("Type", "BD_Label"));
                    // serializedItem.Add(new XElement("Text", (item as Label).Text));
                    break;
                case "BD_TextBox":
                    serializedItem.Add(new XElement("FontBold", (item as BD_TextBox).FontBold));
                    //  serializedItem.Add(new XElement("FontSize", (item as BD_TextBox).Font.Size));
                    serializedItem.Add(new XElement("FontItalic", (item as BD_TextBox).FontItalic));
                    serializedItem.Add(new XElement("TextAlign", (item as BD_TextBox).TextAlign));
                    serializedItem.Add(new XElement("Limit", (item as BD_TextBox).Limit));
                    serializedItem.Add(new XElement("Limittype", (item as BD_TextBox).Limittype));
                    serializedItem.Add(new XElement("Type", "BD_TextBox"));
                    serializedItem.Add(new XElement("InputType", (item as BD_TextBox).InputType));
                    serializedItem.Add(new XElement("ArgName", (item as BD_TextBox).ArgName));
                    serializedItem.Add(new XElement("AutoBind", (item as BD_TextBox).AutoBind));
                    serializedItem.Add(new XElement("Formular", (item as BD_TextBox).Formular));
                    // serializedItem.Add(new XElement("Text", (item as BD_TextBox).Text));
                    break;
                case "BD_RichTextBox":
                    serializedItem.Add(new XElement("FontBold", (item as BD_RichTextBox).FontBold));
                    // serializedItem.Add(new XElement("FontSize", (item as BD_RichTextBox).Font.Size));
                    serializedItem.Add(new XElement("FontItalic", (item as BD_RichTextBox).FontItalic));
                    serializedItem.Add(new XElement("Limit", (item as BD_RichTextBox).Limit));
                    serializedItem.Add(new XElement("Limittype", (item as BD_RichTextBox).Limittype));
                    serializedItem.Add(new XElement("Type", "BD_RichTextBox"));
                    //serializedItem.Add(new XElement("Value", (item as RichTextBox).Text));
                    break;
                case "BD_Button":
                    serializedItem.Add(new XElement("FontBold", (item as BD_Button).FontBold));
                    //   serializedItem.Add(new XElement("FontSize", (item as BD_Button).Font.Size));
                    serializedItem.Add(new XElement("FontItalic", (item as BD_Button).FontItalic));
                    serializedItem.Add(new XElement("TextAlign", (item as BD_Button).TextAlign));
                    serializedItem.Add(new XElement("Type", "BD_Button"));
                    break;
                case "BD_RadioButton":
                    serializedItem.Add(new XElement("FontBold", (item as BD_RadioButton).FontBold));
                    //  serializedItem.Add(new XElement("FontSize", (item as BD_RadioButton).Font.Size));
                    serializedItem.Add(new XElement("FontItalic", (item as BD_RadioButton).FontItalic));
                    serializedItem.Add(new XElement("Type", "BD_RadioButton"));
                    serializedItem.Add(new XElement("Checked", (item as RadioButton).Checked));
                    break;
                case "BD_CheckBox":
                    serializedItem.Add(new XElement("FontBold", (item as BD_CheckBox).FontBold));
                    //  serializedItem.Add(new XElement("FontSize", (item as BD_CheckBox).Font.Size));
                    serializedItem.Add(new XElement("FontItalic", (item as BD_CheckBox).FontItalic));
                    serializedItem.Add(new XElement("Type", "BD_CheckBox"));
                    serializedItem.Add(new XElement("Checked", (item as CheckBox).Checked));
                    break;
                case "BD_ComboBox":
                    serializedItem.Add(new XElement("FontBold", (item as BD_ComboBox).FontBold));
                    //   serializedItem.Add(new XElement("FontSize", (item as BD_ComboBox).Font.Size));
                    serializedItem.Add(new XElement("FontItalic", (item as BD_ComboBox).FontItalic));
                    serializedItem.Add(new XElement("Type", "BD_ComboBox"));
                    serializedItem.Add(new XElement("AutoBind", (item as BD_ComboBox).AutoBind));

                    value = new StringBuilder();
                    BD_ComboBox cb = item as BD_ComboBox;
                    if (!String.IsNullOrEmpty(cb.SQL))
                    {
                        serializedItem.Add(new XElement("SQL", cb.SQL));
                    }
                    else
                    {
                        for (int i = 0; i < cb.Items.Count; i++)
                        {
                            value.Append(":" + cb.Items[i].ToString());
                        }
                        serializedItem.Add(new XElement("Items", value));
                    }
                    //value = value.Substring(0, value.Length);
                    break;

                case "BD_PictureBox":
                    serializedItem.Add(new XElement("FontBold", (item as BD_PictureBox).FontBold));
                    //       serializedItem.Add(new XElement("FontSize", (item as BD_PictureBox).FontSize));
                    serializedItem.Add(new XElement("FontItalic"), (item as BD_PictureBox).FontItalic);
                    serializedItem.Add(new XElement("Type", "BD_PictureBox"));
                    serializedItem.Add(new XElement("Image", (item as BD_PictureBox).ImageLocation));
                    break;

                case "BD_DateTimePicker":
                    serializedItem.Add(new XElement("FontBold", (item as BD_DateTimePicker).FontBold));
                    //    serializedItem.Add(new XElement("FontSize", (item as BD_DateTimePicker).FontSize));
                    serializedItem.Add(new XElement("FontItalic", (item as BD_DateTimePicker).FontItalic));
                    serializedItem.Add(new XElement("Type", "BD_DateTimePicker"));
                    BD_DateTimePicker Tpc = item as BD_DateTimePicker;
                    XElement xmlDataTimePicker = new XElement("TimePicker");
                    xmlDataTimePicker.Add(new XElement("TimeValue", Tpc.Value.ToString("yyyy-MM-dd")));
                    serializedItem.Add(xmlDataTimePicker);
                    break;
                case "BD_Horizon":
                    serializedItem.Add(new XElement("FontBold", (item as BD_Horizon).FontBold));
                    //    serializedItem.Add(new XElement("FontSize", (item as BD_DateTimePicker).FontSize));
                    serializedItem.Add(new XElement("FontItalic", (item as BD_Horizon).FontItalic));
                    serializedItem.Add(new XElement("Type", "BD_Horizon"));
                    break;
                case "BD_Vertical":
                    serializedItem.Add(new XElement("FontBold", (item as BD_Vertical).FontBold));
                    //    serializedItem.Add(new XElement("FontSize", (item as BD_DateTimePicker).FontSize));
                    serializedItem.Add(new XElement("FontItalic", (item as BD_Vertical).FontItalic));
                    serializedItem.Add(new XElement("Type", "BD_Vertical"));
                    break;
                case "BD_DataGridView":
                    serializedItem.Add(new XElement("FontBold", (item as BD_DataGridView).FontBold));
                    //     serializedItem.Add(new XElement("FontSize", (item as BD_DataGridView).Font.Size));
                    serializedItem.Add(new XElement("FontItalic", (item as BD_DataGridView).FontItalic));
                    serializedItem.Add(new XElement("Type", "BD_DataGridView"));
                    value = new StringBuilder();
                    BD_DataGridView Grv = item as BD_DataGridView;
                    XElement xmlDataGridView = new XElement("GridView");
                    String operType = Grv.OperType.ToString();
                    xmlDataGridView.Add(new XElement("OperType", operType));
                    if (operType == "绑定数据库")
                    {
                        xmlDataGridView.Add(new XElement("SQL", Grv.SQL));
                    }
                    else
                    {
                        XElement xmlColumns = new XElement("Columns");
                        XElement xmlRows = new XElement("Rows");
                        this.ProduDTWithData(Grv, ref xmlColumns, ref xmlRows);
                        xmlDataGridView.Add(xmlColumns);
                        xmlDataGridView.Add(xmlRows);
                    }
                    serializedItem.Add(xmlDataGridView);
                    break;
                case "BD_TreeView_Employee":
                    serializedItem.Add(new XElement("FontBold", (item as BD_TreeView_Employee).FontBold));
                    serializedItem.Add(new XElement("FontItalic", (item as BD_TreeView_Employee).FontItalic));
                    serializedItem.Add(new XElement("Type", "BD_TreeView_Employee"));
                    break;
                default:
                    return;
            }
            //serializedItem.Add(new XElement("Name", "C" + numName.ToString()));
            serializedItem.Add(new XElement("Name", item.Name));
            numName++;
            serializedPageItems.Add(serializedItem);
        }

        private void ProduDTWithData(BD_DataGridView grdView, ref XElement xmlColumns, ref XElement xmlRows)
        {
            var colCount = grdView.Columns.Count;
            var columns = grdView.Columns;
            for (int colIndex = 0; colIndex < colCount; colIndex++)
            {
                var column = columns[colIndex] as ExternPropTextBoxColumn;
                var colType = column.CellType.FullName.Replace("Cell", "Column");
                XElement xmlColumn = new XElement("ColTitle", column.HeaderText);
                xmlColumn.SetAttributeValue("ColType", colType);
                xmlColumn.SetAttributeValue("ColName", column.Name);
                xmlColumn.SetAttributeValue("Fillable", column.Fillable.ToString());
                xmlColumn.SetAttributeValue("Gather", column.Gather);
                xmlColumns.Add(xmlColumn);
            }

            var rowCount = grdView.Rows.Count - 1;
            for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
            {
                XElement xmlRow = new XElement("Row", this.getRowData(colCount, grdView.Rows[rowIndex]));
                xmlRows.Add(xmlRow);
            }
        }

        private String getRowData(Int32 colCount, DataGridViewRow row)
        {
            String rowValue = String.Empty;
            for (int colIndex = 0; colIndex < colCount; colIndex++)
                rowValue += row.Cells[colIndex].Value + ";";
            return rowValue;
        }

        #endregion

        #region xml转化为web格式
        ControlDefinitionHelper wcd = null;
        StringBuilder sb = null;
        /// <summary>
        /// 反序列化控件定义,返回web下的定义字符串
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        private string deserializeXmlToWeb(XElement xml, String sqlargs, Boolean readOnly, SerializeAutoData sad)
        {
            wcd = new ControlDefinitionHelper(readOnly);
            sb = new StringBuilder();
            //sb.Append("<div style=\"height:200px ;position: absolute; top: 50px;\"> ");

            var formName = xml.Attribute("FormName").Value;

            var index = 0;
            var pageSb = new StringBuilder();
            //pageSb.Append(wcd.getWeb_form(formName));

            var chilElems = xml.Elements();

            foreach (var chilElem in chilElems)
            {
                int maxheight = chilElem.Attribute("maxHeight") != null ? int.Parse(chilElem.Attribute("maxHeight").Value) : 800;//默认高度，如果没有获得高度值，则默认从800高度开始加载审批结果数据
                foreach (XElement f in chilElem.Elements())
                {
                    //if (f.Element("Type").Value == "Form")
                    //{
                    //  sb.Append(wcd.getWeb_form(f));
                    //}
                    //else 
                    if (f.Element("Type").Value == "BD_Label")
                    {
                        pageSb.Append(wcd.getWeb_label(f));
                    }
                    else if (f.Element("Type").Value == "BD_Button")
                    {
                        pageSb.Append(wcd.getWeb_button(f));
                    }
                    else if (f.Element("Type").Value == "BD_TextBox")
                    {
                        pageSb.Append(wcd.getWeb_textbox(f, sad));
                    }
                    else if (f.Element("Type").Value == "BD_RichTextBox")
                    {
                        pageSb.Append(wcd.getWeb_textarea(f));
                    }
                    else if (f.Element("Type").Value == "BD_CheckBox")
                    {
                        pageSb.Append(wcd.getWeb_checkbox(f));
                    }
                    else if (f.Element("Type").Value == "BD_RadioButton")
                    {
                        pageSb.Append(wcd.getWeb_radio(f));
                    }
                    else if (f.Element("Type").Value == "BD_ComboBox")
                    {
                        pageSb.Append(wcd.getWeb_select(f, sad));
                    }
                    else if (f.Element("Type").Value == "BD_PictureBox")
                    {
                        pageSb.Append(wcd.getWeb_image(f));
                    }
                    else if (f.Element("Type").Value == "BD_DateTimePicker")
                    {
                        pageSb.Append(wcd.getWeb_calender(f));
                    }
                    else if (f.Element("Type").Value == "BD_Horizon")
                    {
                        pageSb.Append(wcd.getWeb_horizon(f));
                    }
                    else if (f.Element("Type").Value == "BD_Vertical")
                    {
                        pageSb.Append(wcd.getWeb_vertical(f));
                    }
                    else if (f.Element("Type").Value == "BD_DataGridView")
                    {
                        if (String.IsNullOrEmpty(sqlargs))
                            pageSb.Append(wcd.getWeb_table(f));
                        else
                        {
                            pageSb.Append(wcd.getWeb_table(f, sqlargs));
                        }
                    }
                    else if (f.Element("Type").Value == "BD_TreeView_Employee")
                    {
                        pageSb.Append(wcd.getWeb_treeviewEmployee(f));
                    }
                }
                //如果传入参数有审批结果而且当前是最后一页
                if (null != sad && sad.spyjList != null && chilElems.Last() == chilElem)
                {
                    for (int i = 0; i < sad.spyjList.Count; i++)
                    {
                        pageSb.Append(wcd.getWeb_Spjg(sad.spyjList[i], maxheight + i * 70));//70为2条结果的高度差
                    }
                }
                sb.Append(packagePage(pageSb.ToString(), index, null));
                //sb.Append("<div style='page-break-after:always;'></div>");
                //sb.Append(pageSb);
                pageSb.Remove(0, pageSb.Length);
                index++;
            }
            //sb.Append("</div > ");
            return sb.ToString();
        }

        private String packagePage(String page, Int32 index, String width)
        {
            var pageBox = String.Empty;
            if (String.IsNullOrEmpty(width))
                width = "595";
            pageBox += "<div class='page' style='position: absolute ; top:" + 790 * index + "px; width:" + width + "px; height:790px; '>";
            pageBox += page;
            pageBox += "</div>";
            return pageBox;
        }

        public String DeserializeXmlToWeb(XElement xml, String sqlargs, SerializeAutoData sad)
        {
            return deserializeXmlToWeb(xml, sqlargs, false, sad);
        }

        public String DeserializeXmlToWeb(XElement xml, SerializeAutoData sad)
        {
            return deserializeXmlToWeb(xml, null, false, sad);
        }

        public String DeserializeXmlToWeb(XElement xml, Boolean readOnly, SerializeAutoData sad)
        {
            return deserializeXmlToWeb(xml, null, readOnly, sad);
        }

        public String DeserializeXmlToWeb(XElement xml, String sqlargs, Boolean readOnly, SerializeAutoData sad)
        {
            return deserializeXmlToWeb(xml, sqlargs, readOnly, sad);
        }

        public String GetTableWithData(String[] txtValue, String sql)
        {
            wcd = new ControlDefinitionHelper();
            return wcd.produTableBySql(sql, txtValue);
        }

        public XElement FillInsValueToXml(XElement xml, List<WfInsvalueEntity> WfInsvalueEntitys)
        {
            if (WfInsvalueEntitys == null) return xml;    //mlh 2012-03-31 + 

            wcd = new ControlDefinitionHelper();
            sb = new StringBuilder();
            //sb.Append("<div style=\"height:200px ;position: absolute; top: 50px;\"> ");


            for (int i = 0; i < WfInsvalueEntitys.Count; i++)
            {
                var chilElems = xml.Elements();
                foreach (var chilElemt in chilElems)
                {
                    foreach (XElement f in chilElemt.Elements())
                    {
                        //if (f.Element("Type").Value == "Form")
                        //{

                        //}
                        //else 
                        if (f.Element("Type").Value == "BD_Label")
                        {

                        }
                        else if (f.Element("Type").Value == "BD_Button")
                        {

                        }
                        else if (f.Element("Type").Value == "BD_TextBox")
                        {
                            if (f.Element("Name").Value == WfInsvalueEntitys[i].fieldName)
                            {
                                f.SetElementValue("Text", WfInsvalueEntitys[i].fieldValue);
                            }
                        }
                        else if (f.Element("Type").Value == "BD_RichTextBox")
                        {
                            if (f.Element("Name").Value == WfInsvalueEntitys[i].fieldName)
                            {
                                f.SetElementValue("Text", WfInsvalueEntitys[i].fieldValue);
                            }
                        }
                        else if (f.Element("Type").Value == "BD_CheckBox")
                        {
                            if (f.Element("Name").Value == WfInsvalueEntitys[i].fieldName)
                            {
                                var check = WfInsvalueEntitys[i].fieldValue == "on" ? true : false;
                                f.SetElementValue("Checked", check);
                            }
                        }
                        else if (f.Element("Type").Value == "BD_RadioButton")
                        {

                        }
                        else if (f.Element("Type").Value == "BD_ComboBox")
                        {
                            if (f.Element("Name").Value == WfInsvalueEntitys[i].fieldName)
                            {
                                f.SetElementValue("Items", WfInsvalueEntitys[i].fieldValue);
                            }
                        }
                    }
                }


            }
            return xml;
        }

        public String DeserializeXmlToWebForSalary(XElement xml)
        {
            ControlDefinitionHelperForSalary wcds = new ControlDefinitionHelperForSalary();
            sb = new StringBuilder();
            //sb.Append("<div style=\"height:200px ;position: absolute; top: 50px;\"> ");

            var formName = xml.Attribute("FormName").Value;

            var index = 0;
            var pageSb = new StringBuilder();
            //pageSb.Append(wcd.getWeb_form(formName));

            var chilElems = xml.Elements();

            foreach (var chilElem in chilElems)
            {
                int maxheight = chilElem.Attribute("maxHeight") != null ? int.Parse(chilElem.Attribute("maxHeight").Value) : 800;//默认高度，如果没有获得高度值，则默认从800高度开始加载审批结果数据
                foreach (XElement f in chilElem.Elements())
                {
                    //if (f.Element("Type").Value == "Form")
                    //{
                    //  sb.Append(wcd.getWeb_form(f));
                    //}
                    //else 
                    if (f.Element("Type").Value == "BD_Label")
                    {
                        pageSb.Append(wcds.getWeb_label(f));
                    }
                    else if (f.Element("Type").Value == "BD_DataGridView")
                    {
                        pageSb.Append(wcds.getWeb_table(f));
                    }
                }

                sb.Append(packagePage(pageSb.ToString(), index, "1500"));
                //sb.Append("<div style='page-break-after:always;'></div>");
                //sb.Append(pageSb);
                pageSb.Remove(0, pageSb.Length);
                index++;
            }
            //sb.Append("</div > ");
            return sb.ToString();
        }

        #endregion

        #region xml转化为entity
        public FormEntity DeserializeXmlToFormEntity(XElement xml)
        {
            FormEntity formEntity = new FormEntity();

            var formName = xml.Attribute("FormName").Value;
            formEntity.name = formName;
            var chilElems = xml.Elements();

            foreach (var chilElem in chilElems)
            {
                foreach (XElement f in chilElem.Elements())
                {
                    //if (f.Element("Type").Value == "Form")
                    //{
                    //    formEntity.name = f.Element("Name").Value;

                    //}
                    //else 
                    if (f.Element("Type").Value == "BD_Label")
                    {
                        FormFieldEntity ffe = new FormFieldEntity()
                        {
                            descn = "",
                            fieldtype = "string",
                            form_id = -1,
                            formfield_id = -1,
                            inputfrom = "",
                            valuedefault = f.Element("Text").Value,
                            name = f.Element("Name").Value,
                            webtype = "label",
                            formtype = "Label",
                            isquery = 0,
                            fromvalue = "",
                            valuescale = "",
                            fromparams = "",
                        };
                        formEntity.formfieldEntityList.Add(ffe);
                    }
                    else if (f.Element("Type").Value == "BD_Button")
                    {
                        FormFieldEntity ffe = new FormFieldEntity()
                        {
                            descn = "",
                            fieldtype = "string",
                            form_id = -1,
                            formfield_id = -1,
                            inputfrom = "",
                            valuedefault = f.Element("Text").Value,
                            name = f.Element("Name").Value,
                            webtype = "Button",
                            formtype = "button",
                            isquery = 0,
                            fromvalue = "",
                            valuescale = "",
                            fromparams = "",
                        };
                        formEntity.formfieldEntityList.Add(ffe);
                    }
                    else if (f.Element("Type").Value == "BD_TextBox")
                    {
                        FormFieldEntity ffe = new FormFieldEntity()
                        {
                            descn = "",
                            fieldtype = "string",
                            form_id = -1,
                            formfield_id = -1,
                            inputfrom = "",
                            valuedefault = f.Element("Text").Value,
                            name = f.Element("Name").Value,
                            webtype = "TextBox",
                            formtype = "text",
                            isquery = 0,
                            fromvalue = "",
                            valuescale = "",
                            fromparams = "",
                            ifwfjudge = string.IsNullOrEmpty(f.Element("ArgName").Value) ? "" : f.Element("ArgName").Value,
                        };
                        formEntity.formfieldEntityList.Add(ffe);
                    }
                    else if (f.Element("Type").Value == "BD_RichTextBox")
                    {
                        FormFieldEntity ffe = new FormFieldEntity()
                        {
                            descn = "",
                            fieldtype = "string",
                            form_id = -1,
                            formfield_id = -1,
                            inputfrom = "",
                            valuedefault = f.Element("Text").Value,
                            name = f.Element("Name").Value,
                            webtype = "RichTextBox",
                            formtype = "textarea",
                            isquery = 0,
                            fromvalue = "",
                            valuescale = "",
                            fromparams = "",
                        };
                        formEntity.formfieldEntityList.Add(ffe);
                    }
                    else if (f.Element("Type").Value == "BD_CheckBox")
                    {
                        FormFieldEntity ffe = new FormFieldEntity()
                        {
                            descn = "",
                            fieldtype = "string",
                            form_id = -1,
                            formfield_id = -1,
                            inputfrom = "",
                            valuedefault = f.Element("Text").Value,
                            name = f.Element("Name").Value,
                            webtype = "CheckBox",
                            formtype = "checkbox",
                            isquery = 0,
                            fromvalue = "",
                            valuescale = "",
                            fromparams = "",
                        };
                        formEntity.formfieldEntityList.Add(ffe);
                    }
                    else if (f.Element("Type").Value == "BD_RadioButton")
                    {
                        FormFieldEntity ffe = new FormFieldEntity()
                        {
                            descn = "",
                            fieldtype = "string",
                            form_id = -1,
                            formfield_id = -1,
                            inputfrom = "",
                            valuedefault = f.Element("Text").Value,
                            name = f.Element("Name").Value,
                            webtype = "RadioButton",
                            formtype = "radio",
                            isquery = 0,
                            fromvalue = "",
                            valuescale = "",
                            fromparams = "",
                        };
                        formEntity.formfieldEntityList.Add(ffe);
                    }
                    else if (f.Element("Type").Value == "BD_ComboBox")
                    {
                        FormFieldEntity ffe = new FormFieldEntity()
                        {
                            descn = "",
                            fieldtype = "string",
                            form_id = -1,
                            formfield_id = -1,
                            inputfrom = "",
                            valuedefault = f.Element("Text").Value,
                            name = f.Element("Name").Value,
                            webtype = "ComboBox",
                            formtype = "select",
                            isquery = 0,
                            fromvalue = "",
                            valuescale = "",
                            fromparams = "",
                        };
                        formEntity.formfieldEntityList.Add(ffe);
                    }
                }
            }

            return formEntity;
        }

        #endregion

        #region xml转换成Control

        public void DeserializeXmlToFrmCtrl(XElement xml, IDesignerHost host)
        {
            Control root = (Control)host.RootComponent;
            var ctrls = xml.Elements().ToArray();
            var ctrlCount = ctrls.Count();
            for (int ctrlIndex = 0; ctrlIndex < ctrlCount; ctrlIndex++)
            {
                var currentCtrl = ctrls[ctrlIndex];

                var ctrlName = currentCtrl.Element("Name").Value;
                var ctrlTypeName = currentCtrl.Element("Type").Value;
                var ctrlType = Type.GetType("CommonLibrary.CustControls." + ctrlTypeName);
                var asd = Activator.CreateInstance(ctrlType);

                Control ctrl = new Control();

                #region 生成控件并设置参数

                var fontBold = Boolean.Parse(string.IsNullOrEmpty(currentCtrl.Element("FontBold").Value) ? "false" : currentCtrl.Element("FontBold").Value);
                var fontItalic = Boolean.Parse(string.IsNullOrEmpty(currentCtrl.Element("FontItalic").Value) ? "false" : currentCtrl.Element("FontItalic").Value);

                switch (ctrlTypeName)
                {
                    case "BD_Label":
                        var bdLable = (BD_Label)host.CreateComponent(ctrlType, ctrlName);
                        bdLable.FontSize = float.Parse(currentCtrl.Element("FontSize").Value) - 3;
                        bdLable.FontBold = fontBold;
                        bdLable.FontItalic = fontItalic;
                        if (currentCtrl.Element("TextAlign") != null)
                            bdLable.TextAlign = (textAlign)Enum.Parse(typeof(textAlign), currentCtrl.Element("TextAlign").Value);
                        ctrl = bdLable;
                        break;
                    case "BD_TextBox":
                        var bdTextBox = (BD_TextBox)host.CreateComponent(ctrlType, ctrlName);
                        bdTextBox.FontSize = float.Parse(currentCtrl.Element("FontSize").Value) - 3;
                        bdTextBox.FontBold = fontBold;
                        bdTextBox.FontItalic = fontItalic;
                        if (currentCtrl.Element("TextAlign") != null)
                            bdTextBox.TextAlign = (textAlign)Enum.Parse(typeof(textAlign), currentCtrl.Element("TextAlign").Value);
                        bdTextBox.Limit = currentCtrl.Element("Limit").Value;
                        bdTextBox.Limittype = (Limitypes)Enum.Parse(typeof(Limitypes), currentCtrl.Element("Limittype").Value);
                        if (currentCtrl.Element("InputType") != null)
                            bdTextBox.InputType = (InputType)Enum.Parse(typeof(InputType), currentCtrl.Element("InputType").Value);
                        bdTextBox.ArgName = currentCtrl.Element("ArgName").Value;
                        if (currentCtrl.Element("Formular") != null)
                            bdTextBox.Formular = currentCtrl.Element("Formular").Value;
                        //自动绑定参数 的属性
                        if (null != currentCtrl.Element("AutoBind"))
                        {
                            if (CommonLibrary.CustControls.BD_TextBox.autobing.绑定到部门.ToString() == currentCtrl.Element("AutoBind").Value)
                            {
                                bdTextBox.AutoBind = CommonLibrary.CustControls.BD_TextBox.autobing.绑定到部门;
                            }
                            else if (CommonLibrary.CustControls.BD_TextBox.autobing.绑定到发起人.ToString() == currentCtrl.Element("AutoBind").Value)
                            {
                                bdTextBox.AutoBind = CommonLibrary.CustControls.BD_TextBox.autobing.绑定到发起人;
                            }
                            else if (CommonLibrary.CustControls.BD_TextBox.autobing.绑定到日期.ToString() == currentCtrl.Element("AutoBind").Value)
                            {
                                bdTextBox.AutoBind = CommonLibrary.CustControls.BD_TextBox.autobing.绑定到日期;
                            }
                            else if (CommonLibrary.CustControls.BD_TextBox.autobing.绑定到编号.ToString() == currentCtrl.Element("AutoBind").Value)
                            {
                                bdTextBox.AutoBind = CommonLibrary.CustControls.BD_TextBox.autobing.绑定到编号;
                            }
                            else if (CommonLibrary.CustControls.BD_TextBox.autobing.绑定到职位.ToString() == currentCtrl.Element("AutoBind").Value)
                            {
                                bdTextBox.AutoBind = CommonLibrary.CustControls.BD_TextBox.autobing.绑定到职位;
                            }
                        }
                        ctrl = bdTextBox;
                        break;
                    case "BD_RichTextBox":
                        var bdRichTextBox = (BD_RichTextBox)host.CreateComponent(ctrlType, ctrlName);
                        bdRichTextBox.FontSize = float.Parse(currentCtrl.Element("FontSize").Value) - 3;
                        bdRichTextBox.FontBold = fontBold;
                        bdRichTextBox.FontItalic = fontItalic;
                        bdRichTextBox.Limit = currentCtrl.Element("Limit").Value;
                        bdRichTextBox.Limittype = (Limitypes)Enum.Parse(typeof(Limitypes), currentCtrl.Element("Limittype").Value);

                        ctrl = bdRichTextBox;
                        break;
                    case "BD_Button":
                        var bdButton = (BD_Button)host.CreateComponent(ctrlType, ctrlName);
                        bdButton.FontSize = float.Parse(currentCtrl.Element("FontSize").Value) - 3;
                        bdButton.FontBold = fontBold;
                        bdButton.FontItalic = fontItalic;
                        if (currentCtrl.Element("TextAlign") != null)
                            bdButton.TextAlign = (textAlign)Enum.Parse(typeof(textAlign), currentCtrl.Element("TextAlign").Value);
                        ctrl = bdButton;
                        break;
                    case "BD_CheckBox":
                        var bdCheckBox = (BD_CheckBox)host.CreateComponent(ctrlType, ctrlName);
                        bdCheckBox.FontSize = float.Parse(currentCtrl.Element("FontSize").Value) - 3;
                        bdCheckBox.FontBold = fontBold;
                        bdCheckBox.FontItalic = fontItalic;
                        bdCheckBox.Checked = Boolean.Parse(currentCtrl.Element("Checked").Value);
                        ctrl = bdCheckBox;
                        break;
                    case "BD_RadioButton":
                        var bdRadioButton = (BD_RadioButton)host.CreateComponent(ctrlType, ctrlName);
                        bdRadioButton.FontSize = float.Parse(currentCtrl.Element("FontSize").Value) - 3;
                        bdRadioButton.FontBold = fontBold;
                        bdRadioButton.FontItalic = fontItalic;
                        bdRadioButton.Checked = Boolean.Parse(currentCtrl.Element("Checked").Value);
                        ctrl = bdRadioButton;
                        break;
                    case "BD_ComboBox":
                        var bdComboBox = (BD_ComboBox)host.CreateComponent(ctrlType, ctrlName);
                        bdComboBox.FontSize = float.Parse(currentCtrl.Element("FontSize").Value) - 3;
                        bdComboBox.FontBold = fontBold;
                        bdComboBox.FontItalic = fontItalic;
                        if (currentCtrl.Element("SQL") != null)
                        {
                            bdComboBox.SQL = currentCtrl.Element("SQL").Value;
                        }
                        else
                        {
                            var items = CommonMethod.splitString(currentCtrl.Element("Items").Value, ':');
                            foreach (var item in items)
                            {
                                bdComboBox.Items.Add(item);
                            }
                        }
                        if (null != currentCtrl.Element("AutoBind"))
                        {
                            if (currentCtrl.Element("AutoBind").Value == CommonLibrary.CustControls.BD_ComboBox.autobing.绑定到本部门人员.ToString())
                                bdComboBox.AutoBind = CommonLibrary.CustControls.BD_ComboBox.autobing.绑定到本部门人员;
                            else if (currentCtrl.Element("AutoBind").Value == CommonLibrary.CustControls.BD_ComboBox.autobing.绑定到本公司职位.ToString())
                                bdComboBox.AutoBind = CommonLibrary.CustControls.BD_ComboBox.autobing.绑定到本公司职位;
                        }

                        ctrl = bdComboBox;
                        break;
                    case "BD_PictureBox":
                        var bdPictureBox = (BD_PictureBox)host.CreateComponent(ctrlType, ctrlName);
                        bdPictureBox.FontSize = float.Parse(currentCtrl.Element("FontSize").Value) - 3;
                        bdPictureBox.FontBold = fontBold;
                        bdPictureBox.FontItalic = fontItalic;
                        bdPictureBox.ImageLocation = currentCtrl.Element("Image").Value;
                        ctrl = bdPictureBox;
                        break;
                    case "BD_DateTimePicker":
                        var bdDateTimePicker = (BD_DateTimePicker)host.CreateComponent(ctrlType, ctrlName);
                        bdDateTimePicker.FontSize = float.Parse(currentCtrl.Element("FontSize").Value) - 3;
                        bdDateTimePicker.FontBold = fontBold;
                        bdDateTimePicker.FontItalic = fontItalic;
                        bdDateTimePicker.Value = DateTime.Parse(currentCtrl.Element("TimePicker").Element("TimeValue").Value);
                        ctrl = bdDateTimePicker;
                        break;
                    case "BD_DataGridView":
                        var bdDataGridView = (BD_DataGridView)host.CreateComponent(ctrlType, ctrlName);
                        bdDataGridView.FontSize = float.Parse(currentCtrl.Element("FontSize").Value) - 3;
                        bdDataGridView.FontBold = fontBold;
                        bdDataGridView.FontItalic = fontItalic;
                        var gridViewXml = currentCtrl.Element("GridView");
                        var operType = gridViewXml.Element("OperType").Value;

                        bdDataGridView.OperType = (BD_DataGridView.oprType)Enum.Parse(typeof(BD_DataGridView.oprType), operType);

                        if (operType == "绑定数据库")
                        {
                            bdDataGridView.SQL = currentCtrl.Element("SQL").Value;
                        }
                        else
                        {
                            var colsXml = gridViewXml.Element("Columns").Elements("ColTitle");
                            DataGridView innerGrdView = bdDataGridView;
                            foreach (var colXml in colsXml)
                            {
                                ExternPropTextBoxColumn col = new ExternPropTextBoxColumn();
                                col.Fillable = Boolean.Parse(colXml.Attribute("Fillable").Value);
                                col.Gather = colXml.Attribute("Gather").Value;
                                col.Name = colXml.Attribute("ColName").Value;
                                col.HeaderText = colXml.Value;
                                col.DisplayIndex = innerGrdView.Columns.Count;
                                innerGrdView.Columns.Add(col);
                                bdDataGridView.Columns = innerGrdView.Columns;
                            }
                            var colCount = colsXml.Count();
                            var rowsXml = gridViewXml.Element("Rows").Elements("Row");
                            foreach (var rowXml in rowsXml)
                            {
                                var rowValues = CommonMethod.splitString(rowXml.Value);
                                bdDataGridView.Rows.Add(rowValues);
                            }
                        }
                        ctrl = bdDataGridView;
                        break;
                    case "BD_Horizon":
                        var bdHorizon = (BD_Horizon)host.CreateComponent(ctrlType, ctrlName);
                        bdHorizon.FontSize = float.Parse(currentCtrl.Element("FontSize").Value) - 3;
                        bdHorizon.FontBold = fontBold;
                        bdHorizon.FontItalic = fontItalic;
                        ctrl = bdHorizon;
                        break;
                    case "BD_Vertical":
                        var bdVertical = (BD_Vertical)host.CreateComponent(ctrlType, ctrlName);
                        bdVertical.FontSize = float.Parse(currentCtrl.Element("FontSize").Value) - 3;
                        bdVertical.FontBold = fontBold;
                        bdVertical.FontItalic = fontItalic;
                        ctrl = bdVertical;
                        break;
                    case "BD_TreeView_Employee":
                        var bdTreeViewEmplyee = (BD_TreeView_Employee)host.CreateComponent(ctrlType, ctrlName);
                        ctrl = bdTreeViewEmplyee;
                        break;
                    default:
                        break;
                }

                ctrl.Name = ctrlName;
                ctrl.Text = currentCtrl.Element("Text").Value;
                ctrl.Left = Int32.Parse(currentCtrl.Element("Left").Value);
                ctrl.Top = Int32.Parse(currentCtrl.Element("Top").Value) - 8;
                ctrl.Width = Int32.Parse(currentCtrl.Element("Width").Value);
                ctrl.Height = Int32.Parse(currentCtrl.Element("Height").Value);

                #endregion

                root.Controls.Add(ctrl);
            }
        }

        #endregion
    }

    #region 参数

    public class SerializeAutoData
    {
        public SerializeAutoData(bool _autobind)
        {
            autobind = _autobind;
        }
        #region textbox

        public string user_name { set; get; }
        public string user_deptname { set; get; }
        public string user_dutyname { set; get; }
        public string user_id { set; get; }
        public bool autobind { set; get; }

        #endregion

        #region combox
        public string[] ComboSelfStaffList = null;
        public string[] ComboAllDutyList = null;
        #endregion

        #region 审批结果

        public List<AutoDataSpjg> spyjList = new List<AutoDataSpjg>();
        #endregion

    }
    public class AutoDataSpjg//审批结果
    {
        public AutoDataSpjg(string _name, string _dept, string _duty, string _spyj, string _sprq, string _spjg)
        {
            name = _name;
            dept = _dept;
            duty = _duty;
            spyj = _spyj;
            sprq = _sprq;
            spjg = _spjg;
        }
        public string name { set; get; }  //审批人
        public string dept { set; get; }   //对应部门
        public string duty { set; get; }   //对应职位
        public string spyj { set; get; }   //对应审批意见
        public string sprq { set; get; }  //对应审批日期
        public string spjg { set; get; }  //对应审批结果
    }

    #endregion

    class ControlDefinitionHelper
    {
        //示例：
        //<div id="d"; style="left:1px; top:1px; height:11px; width:11px;position: absolute;font-family:@华文新魏;
        //     font-size:11px; background:111111; color:Black;  font-style:italic; font-weight:bold "></div>
        #region web
        //原来的
        //string form = " <h1 style=\"text-align: center; position: absolute;top:60px;width:1000px\">表单：\"{0}\"</h1>";
        //string label = " <div id=\"{0}\"  name=\"{0}\" style=\"left:{2}px; top:{3}px;width:{4}px;height:{5}px; position: absolute;font: {6}; font-size: {7}px;font-style:{8}; font-weight:{9};color:{10};background:{11} \">{1}</div>";//一个空的div代替一个label
        //string text = "<input id=\"{0}\"  name=\"{0}\" type=\"text\" style=\"left:{2}px; top:{3}px;width:{4}px;height:{5}px; position: absolute;font: {6}; font-size: {7}px;font-style:{8}; font-weight:{9};color:{10};background:{11} \"  value=\"{1}\"/>";
        //string textarea = "<textarea id=\"{0}\"  name=\"{0}\" type=\"text\" style=\"left:{2}px; top:{3}px;height:{5}px; position: absolute;font: {6}; font-size: {7}px;font-style:{8}; font-weight:{9};color:{10};background:{11};  cols=\"{4}\" >{1}</textarea>";
        //string button = "<input id=\"{0}\" name=\"{0}\" type=\"button\"  style=\"left:{2}px; top:{3}px;width:{4}px;height:{5}px; position: absolute;font: {6}; font-size: {7}px;font-style:{8}; font-weight:{9};color:{10};background:{11} \"  value=\"{1}\"/>";
        //string checkbox = "<div  style=\"left:{2}px; top:{3}px;width:{4}px;height:{5}px; position: absolute;font: {6}; font-size: {7}px;font-style:{8}; font-weight:{9};color:{10};background:{11} \"><input id=\"{0}\" name=\"{0}\" type=\"checkbox\" />{1}</div>";
        //string radio = "<input id=\"{0}\"  name=\"{0}\" type=\"radio\"  style=\"left:{2}px; top:{3}px;width:{4}px;height:{5}px; position: absolute;font: {6}; font-size: {7}px;font-style:{8}; font-weight:{9};color:{10};background:{11} \"  value=\"{1}\"/>";
        //string select = "<select id=\"{0}\" name=\"{0}\"  style=\"left:{2}px; top:{3}px;width:{4}px;height:{5}px; position: absolute;font: {6}; font-size: {7}px;font-style:{8}; font-weight:{9};color:{10};background:{11}\">";
        //string selectValue = "<option  value=\"{0}\">{1}</option>";

        //直接可识别的
        string form = " <h1 style=' text-align:left; position: relative;top:20px;left:20px; '>表单：'{0}'</h1>";
        string label = " <div id='{0}'  name='{0}' style='left:{2}px; top:{3}px;width: {4}px;height:{5}px; position: absolute;font: {6}; font-size: {7}px;font-style:{8}; font-weight:{9};color:{10};background:{11}; text-align:[align]; '>{1}</div>";//一个空的div代替一个label
        string text = "<input id='{0}'  name='{0}' class = 'txtInput txtunderline' type='text' style='left:{2}px; top:{3}px;width: {4}px;height:{5}px; position: absolute;font: {6}; font-style:{8}; font-weight:{9};text-align:[align]; color:{10};background:{11}; ' [formular]  value='{1}' [onblur]  onfocus='limitlength(this); calculate(this);' {12} />";
        string textarea = "<textarea id='{0}' class='bdtextarea'  name='{0}' type='text' style='resize: none; overflow:hidden; left:{2}px; top:{3}px;width:{4}px;height:{5}px; position: absolute;font: {6}; font-size: {7}px;font-style:{8}; font-weight:{9};color:{10};background:{11};' {12} >{1}</textarea>";
        string button = "<input id='{0}' name='{0}' type='button'  style='left:{2}px; top:{3}px;width: {4}px;height:{5}px;  position: absolute;font: {6};text-align:[align]; font-size: {7}px;font-style:{8}; font-weight:{9};color:{10};background:{11}; '  value='{1}'/>";
        string checkbox = "<div class='bdcheckbox' style='left:{2}px; top:{3}px;width: {4}px;height:{5}px; position: absolute;font: {6}; font-size: {7}px;font-style:{8}; font-weight:{9};color:{10};background:{11}; '><input id='{0}' name='{0}' style='margin:3px;' type='checkbox' replace onclick='checkvalue(this)' {13} />{1}</div>";
        string radio = "<div  style='left:{2}px; top:{3}px;width: {4}px;height:{5}px; position: absolute;font: {6}; font-size: {7}px;font-style:{8}; font-weight:{9};color:{10};background:{11}; '><input id='{0}' name='{0}' type='radio' replace />{1}</div>";
        string select = "<select id='{0}' name='{0}'  style='left:{2}px; top:{3}px;width: {4}px;height:25px; position: absolute;font: {6}; font-size: {7}px;font-style:{8}; font-weight:{9};color:{10};background:{11};padding-top:3px;'>";
        string selectValue = "<option  value='{0}'>{1}</option>";
        String image = "<img id='{0}'  name='{0}' type='image' style='left:{2}px; top:{3}px;width: {4}px;height:{5}px; position: absolute;font: {6}; font-size: 14px;font-style:{8}; font-weight:{9};color:{10};background:{11}; ' src='replace'/>";
        String calender = "<input id='{0}'  name='{0}' class='form_datepicker' type='text' style='left:{2}px; top:{3}px;width: 120px;height:{5}px; position: absolute;font: {6};font-style:{8}; font-weight:{9};color:{10};background:{11}; ' value='dataVal' readonly='readonly' />";
        String horizon = "<hr style='left:{2}px; top:{3}px; width: {4}px; position: absolute;' />";
        String vertical = "<div style='left:{2}px; top:{3}px; width: 1px; height:{5}px; position: absolute; border-left:1px solid black;'></div>";
        String table = "<div class='bddivtable' style='left:{2}px; top:{3}px; position:absolute; height:{5}px; border:solid 1px black; overflow:auto;'>sqlBanding<div id='updateTable'>tableBanding</div></div>";
        String treeviewEmployee = "<div id='ztreeEmpoyee' class='ztree' name='{0}' style='left:{2}px; top:{3}px;width: {4}px;height:{5}px; position: absolute;'></div>";

        String hidForTable = "<input type='hidden'class='hidTable' value='{0}'/>";
        String txtForTable = "<input type='text' class='txtTable txtunderline' name='{0}'  />";
        String btnForTable = "<input type='button' onclick='getTable(this)' value='查询' /><br />";

        object[] ob;
        Boolean readOnly;
        //sad 为text控件自动绑定值
        void getParam(XElement f, SerializeAutoData sad)
        {
            var autobing = false;
            var read = String.Empty;
            var disable = String.Empty;

            //处理自动banding
            if (sad != null && sad.autobind && null != f.Element("AutoBind"))
            {
                if (f.Element("AutoBind").Value == CommonLibrary.CustControls.BD_TextBox.autobing.不绑定.ToString())
                {
                    autobing = false;
                }
                else if (f.Element("AutoBind").Value == CommonLibrary.CustControls.BD_TextBox.autobing.绑定到部门.ToString())
                {
                    f.Element("Text").Value = sad.user_deptname;
                    autobing = true;
                }
                else if (f.Element("AutoBind").Value == CommonLibrary.CustControls.BD_TextBox.autobing.绑定到发起人.ToString())
                {
                    f.Element("Text").Value = sad.user_name;
                    autobing = true;
                }
                else if (f.Element("AutoBind").Value == CommonLibrary.CustControls.BD_TextBox.autobing.绑定到日期.ToString())
                {
                    f.Element("Text").Value = DateTime.Now.ToString("yyyy-MM-dd-HH:mm");
                    autobing = true;
                }
                else if (f.Element("AutoBind").Value == CommonLibrary.CustControls.BD_TextBox.autobing.绑定到编号.ToString())
                {
                    f.Element("Text").Value = sad.user_id;
                    autobing = true;
                }
                else if (f.Element("AutoBind").Value == CommonLibrary.CustControls.BD_TextBox.autobing.绑定到职位.ToString())
                {
                    f.Element("Text").Value = sad.user_dutyname;
                    autobing = true;
                }
            }

            if (readOnly || autobing)
            {
                read = "readonly";
                disable = "disabled";
            }

            ob = new object[]{ f.Element("Name").Value, f.Element("Text").Value, 
                //                                      0                                         1                                   
                f.Element("Left").Value, f.Element("Top").Value, f.Element("Width").Value, f.Element("Height").Value,
                //                 2                                  3                                     4                                        5
                f.Element("FontName").Value, f.Element("FontSize").Value,  f.Element("FontItalic").Value=="true"?"italic":"normal",f.Element("FontBold").Value=="true"?"bold":"normal",
                //                6                                        7                                     8                                                                              9
                f.Element("ForeColor").Value ,f.Element("BackColor").Value, read, disable};
            //                   10                                        11                          12   13 

            //checkoox 专用  不知道是对哪个属性了 mlh


        }


        public ControlDefinitionHelper(Boolean readOnly)
        {
            this.readOnly = readOnly;
        }

        public ControlDefinitionHelper()
        {
            this.readOnly = false;
        }

        public string getWeb_form(String formName)
        {
            return string.Format(form, formName);
        }
        public string getWeb_label(XElement f)
        {
            getParam(f, null);
            var labletpl = string.Format(label, ob);
            var align = "left";
            if (f.Element("TextAlign") != null)
                align = f.Element("TextAlign").Value;
            labletpl = labletpl.Replace("[align]", align);
            return labletpl;
        }
        public string getWeb_textbox(XElement f, SerializeAutoData sad)
        {
            getParam(f, sad);
            var textboxtpl = string.Format(text, ob);

            var align = "left";
            if (f.Element("TextAlign") != null)
                align = f.Element("TextAlign").Value;
            textboxtpl = textboxtpl.Replace("[align]", align);
            var inputType = "";
            if (f.Element("InputType") != null && f.Element("InputType").Value == "RMB")
                inputType = "onblur='getCapitalNumber(this);'";
            if (f.Element("Formular") != null)
                textboxtpl = textboxtpl.Replace("[formular]", "formular='" + f.Element("Formular").Value + "'");
            textboxtpl = textboxtpl.Replace("[onblur]", inputType);
            return textboxtpl;
        }
        public string getWeb_textarea(XElement f)
        {
            getParam(f, null);
            return string.Format(textarea, ob);
        }
        public string getWeb_button(XElement f)
        {
            getParam(f, null);
            var buttontpl = string.Format(button, ob);

            var align = "center";
            if (f.Element("TextAlign") != null)
                align = f.Element("TextAlign").Value;
            buttontpl = buttontpl.Replace("[align]", align);
            return buttontpl;
        }

        public string getWeb_checkbox(XElement f)
        {
            getParam(f, null);
            String res = String.Format(checkbox, ob);
            if (f.Element("Checked").Value == "true")
                res = res.Replace("replace", "checked='checked'");
            else
                res = res.Replace("replace", "");
            return res;
        }

        public string getWeb_radio(XElement f)
        {
            getParam(f, null);
            String res = String.Format(radio, ob);
            if (f.Element("Checked").Value == "true")
                res = res.Replace("replace", "checked='checked'");
            else
                res = res.Replace("replace", "");
            return res;
        }

        public string getWeb_select(XElement f, SerializeAutoData sad)
        {
            getParam(f, null);
            string res = string.Format(select, ob);

            string[] strs = new string[1];
            if (f.Element("AutoBind").Value == CommonLibrary.CustControls.BD_ComboBox.autobing.绑定到本部门人员.ToString() && sad != null && sad.ComboSelfStaffList != null)//自动绑定：绑定部门人员
            {
                strs = sad.ComboSelfStaffList;
            }
            else if (f.Element("AutoBind").Value == CommonLibrary.CustControls.BD_ComboBox.autobing.绑定到本公司职位.ToString() && sad != null && sad.ComboAllDutyList != null)//自动绑定：绑定职位列表
            {
                strs = sad.ComboAllDutyList;
            }
            else if (f.Element("Items") != null)// 手动绑定
            {
                strs = f.Element("Items").Value.Split(new Char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            }
            else// SQL绑定
            {
                var sql = f.Element("SQL").Value;
                strs = DB.DBExecuteUtil.queryDataList(sql).ToArray();
            }
        #endregion

            foreach (string s in strs)
            {
                res += (string.Format(selectValue, s, s));
            }

            res += ("</select>");
            return res;
        }

        public String getWeb_image(XElement f)
        {
            getParam(f, null);
            String path = "../../UploadFiles/Form/";
            String res = String.Format(image, ob);
            res = res.Replace("replace", path + (f.Element("Image").Value));
            return res;
        }

        public String getWeb_calender(XElement f)
        {
            getParam(f, null);
            String res = String.Format(calender, ob);
            res = res.Replace("dataVal", f.Element("TimePicker").Element("TimeValue").Value);
            return res;
        }

        public String getWeb_horizon(XElement f)
        {
            getParam(f, null);
            return String.Format(horizon, ob);
        }

        public String getWeb_vertical(XElement f)
        {
            getParam(f, null);
            return String.Format(vertical, ob);
        }

        public String getWeb_table(XElement f)
        {
            return getWeb_table(f, null);
        }
        // 生成table
        public String getWeb_table(XElement f, String sqlargs)
        {
            getParam(f, null);
            String res = String.Format(table, ob);
            String operType = f.Element("GridView").Element("OperType").Value;
            if (operType == "绑定数据库")
            {
                if (String.IsNullOrEmpty(sqlargs))
                {
                    String sql = f.Element("GridView").Element("SQL").Value;
                    var condiCols = Regex.Matches(sql, @"@\w*");
                    if (condiCols.Count == 0)
                    {
                        res = res.Replace("tableBanding", produTableBySql(sql));
                        res = res.Replace("sqlBanding", String.Empty);
                    }
                    else
                    {
                        String sqlTag = sql;
                        foreach (Match condiCol in condiCols)
                        {
                            String col = condiCol.Value.Remove(0, 1);
                            sqlTag = sqlTag.Replace(condiCol.Value.ToString(), String.Format(txtForTable, col));
                        }
                        sqlTag += String.Format(hidForTable, sql);
                        sqlTag += btnForTable;
                        res = res.Replace("tableBanding", "");
                        res = res.Replace("sqlBanding", sqlTag);
                    }
                }
                else
                {
                    res = res.Replace("tableBanding", produTableByData(sqlargs));
                    res = res.Replace("sqlBanding", "");
                }
            }

            if (operType == "手动填写")
            {
                var titles = f.Element("GridView").Element("Columns").Elements("ColTitle").ToArray();
                var rows = f.Element("GridView").Element("Rows").Elements("Row").ToArray();
                var attr = f.Element("GridView").Element("Rows").Attribute("Mark");
                var markNum = "";
                if (attr != null)
                    markNum = attr.Value;
                res = res.Replace("tableBanding", this.produTableByData(titles, rows, sqlargs, false, markNum));
                res = res.Replace("sqlBanding", String.Empty);
            }

            if (operType == "可增表格")
            {
                var titles = f.Element("GridView").Element("Columns").Elements("ColTitle").ToArray();
                var rows = f.Element("GridView").Element("Rows").Elements("Row").ToArray();
                var gatherValue = "";

                for (int titleIndex = 0; titleIndex < titles.Count(); titleIndex++)
                {
                    var gather = titles[titleIndex].Attribute("Gather").Value;
                    if (gather != "None")
                        gatherValue += titleIndex + "-" + gather + ";";
                }
                var gatherHide = "<input id='tbgather' type='hidden' value='" + gatherValue + "' /> tableBanding";
                res = res.Replace("tableBanding", gatherHide);
                res = res.Replace("tableBanding", this.produTableByData(titles, rows, sqlargs, true));
                res = res.Replace("sqlBanding", String.Empty);
            }

            return res;
        }

        public String getWeb_treeviewEmployee(XElement f)
        {
            getParam(f, null);
            return String.Format(treeviewEmployee, ob);
        }

        /**    结果的样式  
        <div class='page' style='position: absolute; top: 0px; width: 595px; height: 790px;'>
            <div  style='left: 40px; top: 330px; width: 80px; height: 21px;
                position: absolute; font: 宋体; font-size: 12px; font-style: normal; font-weight: normal;'>
                审批结果：
            </div>
			<hr/ style='left: 40px; top: 350px; width:500px; 
                position: absolute;'>
		     <div id='lblCheck' name='lblCheck' style='left: 40px; top: 383px; width: 80px; height: 21px;
                position: absolute; font: 宋体; font-size: 12px; font-style: normal; font-weight: normal;text-align: right;'>
                总经理士大飞
            </div>
			   <div id='lbl_zjlsh' name='lbl_zjlsh' style='left: 135px; top: 383px; width: 38px;
                height: 21px; position: absolute; font: 宋体; font-size: 12px; font-style: normal;font-weight: normal; text-align: left;'>
                审核:
            </div>
				<div id='lbl_zjlrq' name='lbl_zjlrq' style='left: 183px; top: 383px; width: 100px;
                height: 21px; position: absolute; font: 宋体; font-size: 12px; font-style: normal;font-weight: normal; text-align: left;'>
                同意地方飞
            </div>
               <div id='lbl_zjlrq' name='lbl_zjlrq' style='left: 329px; top: 383px; width: 64px;
                height: 21px; position: absolute; font: 宋体; font-size: 12px; font-style: normal;font-weight: normal; text-align: left;'>
                审核日期:
            </div>
				<div id='lbl_zjlrq' name='lbl_zjlrq' style='left: 399px; top: 383px; width: 143px;
                height: 21px; position: absolute; font: 宋体; font-size: 12px; font-style: normal;font-weight: normal;text-align: left;'>
                2012-12-04 12:12:12
            </div>
			        <div id='lbl_zjlyj' name='lbl_zjlyj' style='left: 135px; top: 411px; width: 38px;
                height: 21px; position: absolute; font: 宋体; font-size: 12px; font-style: normal;font-weight: normal;text-align: left;'>
                意见:
            </div>
			 <div id='lbl_zjlyj' name='lbl_zjlyj' style='left: 183px; top: 411px; width: 359px;
                height: 21px; position: absolute; font: 宋体; font-size: 12px; font-style: normal; font-weight: normal;text-align: left;'>
                生非斯蒂芬第三方第三方的范德萨发第三方的士大夫士大夫第三方的三多福多寿
            </div>
        </div>
         **/
        //添加审批结果
        string tmp_spjg = "<hr/ style='left: 30px; top: {4}px; width:550px;position: absolute;'>" +
                                 "<div style='left: 40px; top: {5}px; width: 80px; height: 21px;position: absolute; font: 宋体; font-size: 12px; font-style: normal; font-weight: normal;text-align: right;'>{0}</div>" +//name：林稳
                                 "<div style='left: 135px; top: {5}px; width: 38px;height: 21px; position: absolute; font: 宋体; font-size: 12px; font-style: normal;font-weight: normal; text-align: left;'>审核:</div>" +
                                 "<div style='left: 183px; top: {5}px; width: 100px;height: 21px; position: absolute; font: 宋体; font-size: 12px; font-style: normal;font-weight: normal; text-align: left;'>{1}</div>" +//spjg:同意
                                 "<div style='left: 329px; top: {5}px; width: 64px; height: 21px; position: absolute; font: 宋体; font-size: 12px; font-style: normal;font-weight: normal; text-align: left;'> 审核日期:</div>" +
                                 "<div style='left: 399px; top: {5}px; width: 143px;height: 21px; position: absolute; font: 宋体; font-size: 12px; font-style: normal;font-weight: normal;text-align: left;'>{2}</div>" +//sprq:2012-12-04 12:12:12
                                 " <div style='left: 135px; top: {6}px; width: 38px;height: 21px; position: absolute; font: 宋体; font-size: 12px; font-style: normal;font-weight: normal;text-align: left;'>意见:</div>" +
                                 "<div style='left: 183px; top: {6}px; width: 359px;height: 21px; position: absolute; font: 宋体; font-size: 12px; font-style: normal; font-weight: normal;text-align: left;'>{3}</div>";//spyj:申请金额太多，超出等级对应限定
        public String getWeb_Spjg(AutoDataSpjg spjg, int maxheight)
        {
            return string.Format(tmp_spjg, spjg.name, spjg.spjg, spjg.sprq, spjg.spyj, maxheight + 30, maxheight + 50, maxheight + 70);
            //                                                 0                 1           2            3               4                       5                            6 
        }

        public String produTableBySql(String sqlValue, String[] txtValue)
        {
            var sql = produSql(sqlValue, txtValue);
            return produTableBySql(sql);
        }

        // 将sql原句与参数合并 生成最终sql
        private String produTableBySql(String sql)
        {
            TableHelper tableHelper = new TableHelper();
            String table = String.Empty;
            System.Data.DataTable tableDB = DB.DBExecuteUtil.querySqlTable(sql);
            if (tableDB.Rows.Count == 0)
                return "none";
            table += tableHelper.TableBegin();
            table += tableHelper.BodyBegin;
            table += tableHelper.TrBegin;

            foreach (System.Data.DataColumn col in tableDB.Columns)
            {
                var colName = Regex.Match(sql, @"[a-z]* as " + col.ColumnName).ToString();
                colName = colName == "" ? col.ColumnName : colName.Substring(0, colName.Length - (col.ColumnName.Length + 4));
                table += tableHelper.TbTitle(col.Caption, colName);
            }
            table += tableHelper.TrEnd;

            var rowData = DB.DBExecuteUtil.querySqlArray(tableDB);
            for (int rowIndex = 0; rowIndex < rowData.GetLength(0); rowIndex++)
            {
                table += tableHelper.TrBegin;
                foreach (var row in rowData[rowIndex])
                    table += tableHelper.TbLableRow(row);
                table += tableHelper.TrEnd;
            }
            table += tableHelper.BodyEnd;
            table += tableHelper.TableEnd;
            return table;
        }
        private String produTableByData(XElement[] titles, XElement[] rows, String sqlargs, Boolean addable, String markNum)
        {
            TableHelper tableHelper = new TableHelper();
            String table = String.Empty;
            var titCount = titles.Count();
            table += tableHelper.TableBegin();
            table += tableHelper.BodyBegin;
            table += tableHelper.TbTrBegin("tbTitle");
            if (addable)
                table += "<th><lable>编号</lable></th>";
            for (int titIndex = 0; titIndex < titCount; titIndex++)
            {
                table += tableHelper.TbTitle(titles[titIndex].Value, titles[titIndex].Attribute("ColName").Value);
            }
            if (addable)
            {
                table += tableHelper.ThBegin;
                table += tableHelper.TbHyPerLink("<img src='../../images/icons/plus.png'  title='添加一行' style='padding:0 10px' />", "tbaddrow(this)");
                table += tableHelper.TbHyPerLink("<img src='../../images/icons/edit.png' title='合计' style='padding:0 10px' />", "tbgather(this)");
                table += tableHelper.ThEnd;
            }
            table += tableHelper.TrEnd;

            Int32 rowCount;
            String[] sqlRows = new String[1];
            if (!String.IsNullOrEmpty(sqlargs))
            {
                sqlRows = CommonMethod.splitString(sqlargs);
                rowCount = sqlRows.Length;
            }
            else
                rowCount = rows.Count();

            var width = 700 / (titCount + 3);
            String[] num = markNum.Split(new Char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
            {
                if (!String.IsNullOrEmpty(sqlargs))
                {
                    if (rowIndex == 0) continue;
                    var rowValue = sqlRows[rowIndex].Split(',');
                    var colCount = rowValue.Count() - 1;
                    table += tableHelper.TbTrBegin((rowIndex + 1));

                    int colIndex = 0;
                    if (addable)
                    {
                        table += tableHelper.TbLableRow(rowValue[0]);
                        colIndex = 1;
                    }

                    for (; colIndex < colCount; colIndex++)
                    {
                        table += tableHelper.TbLableRow(rowValue[colIndex]);
                    }

                    if (addable)
                        table += tableHelper.TbLableRow("");
                    table += tableHelper.TbTrEnd();

                }
                else
                {
                    var rowValue = rows[rowIndex].Value.Split(';');
                    var colCount = rowValue.Count() - 1;
                    table += tableHelper.TbTrBegin((rowIndex + 1));
                    if (addable)
                        table += tableHelper.TbLableRow((rowIndex + 1).ToString());
                    for (int colIndex = 0; colIndex < colCount; colIndex++)
                    {
                        if (num.Contains(colIndex.ToString()))
                        {
                            table += tableHelper.TbSelectRow(width.ToString(), colIndex.ToString());
                            continue;
                        }

                        var fillable = titles[colIndex].Attribute("Fillable").Value;

                        if (fillable.ToLower() == "true")
                            table += tableHelper.TbTextRow(rowValue[colIndex], width);
                        else
                            table += tableHelper.TbLableRow(rowValue[colIndex]);
                    }
                    if (addable)
                    {
                        table += tableHelper.TdBegin;
                        table += tableHelper.TbHyPerLink("删除", "tbdelrow(this)");
                        table += tableHelper.TdEnd;
                    }
                    table += tableHelper.TbTrEnd();
                }
            }
            table += tableHelper.BodyEnd;
            table += tableHelper.TableEnd;
            return table;

        }


        private String produTableByData(XElement[] titles, XElement[] rows, String sqlargs, Boolean addable)
        {
            return produTableByData(titles, rows, sqlargs, addable, "");
        }

        public String produTableByData(String tableargs)
        {
            TableHelper tableHelper = new TableHelper();
            String table = String.Empty;
            table += tableHelper.TableBegin();
            table += tableHelper.BodyBegin;
            var rows = CommonMethod.splitString(tableargs);
            var rowCount = rows.Count();
            var title = CommonMethod.splitString(rows[0], ',');
            table += tableHelper.TrBegin;
            foreach (String titl in title)
            {
                var titlspl = CommonMethod.splitString(titl, '-');
                table += tableHelper.TbTitle(titlspl[1], titlspl[0] + "-" + titlspl[1]);
            }
            table += tableHelper.TrEnd;

            for (int rowIndex = 1; rowIndex < rowCount; rowIndex++)
            {

                var cols = CommonMethod.splitString(rows[rowIndex], ',');
                table += tableHelper.TrEnd;
                foreach (String col in cols)
                {
                    table += tableHelper.TbLableRow(col);
                }
                table += tableHelper.TrEnd;
            }
            table += tableHelper.BodyEnd;
            table += tableHelper.TableEnd;
            return table;
        }

        // 生成table通过查询数据库带参数
        private String produSql(String sqlValue, String[] txtValue)
        {
            String sql = sqlValue;
            var matchs = Regex.Matches(sqlValue, @"@\w*.");
            for (int i = 0; i < txtValue.GetLength(0); i++)
            {
                String match = matchs[i].ToString();
                if (!String.IsNullOrEmpty(txtValue[i]))
                    sql = sql.Replace(match, "'" + txtValue[i] + "'");
            }
            return sql;
        }



        //#region winform
        //public FormfieldEntity getEntity_form(XElement f)
        //{
        //    return null;
        //}
        //public FormfieldEntity getEntity_label(XElement f)
        //{
        //    return null;

        //}
        //public FormfieldEntity getEntity_button(XElement f)
        //{
        //    return null;
        //}
        //public FormfieldEntity getEntity_textbox(XElement f)
        //{
        //    return null;
        //}
        //public FormfieldEntity getEntity_textarea(XElement f)
        //{
        //    return null;
        //}
        //public FormfieldEntity getEntity_checkbox(XElement f)
        //{
        //    return null;
        //}
        //public FormfieldEntity getEntity_radio(XElement f)
        //{
        //    return null;
        //}

        //public FormfieldEntity getEntity_select(XElement f)
        //{
        //    return null;
        //}
        //#endregion
    }

    class ControlDefinitionHelperForSalary
    {
        String table = "<div class='bddivtable' style='left:{2}px; top:{3}px; position:absolute; height:{5}px; border:solid 1px black; overflow:auto;'><div id='updateTable'>tableBanding</div></div>";
        String label = " <div id='{0}'  name='{0}' style='left:{2}px; top:{3}px;width: {4}px;height:{5}px; position: absolute;font: {6}; font-size: {7}px;font-style:{8}; font-weight:{9};color:{10};background:{11}; text-align:[align]; '>{1}</div>";//一个空的div代替一个label

        object[] ob;
        //sad 为text控件自动绑定值
        void getParam(XElement f)
        {
            ob = new object[]{ f.Element("Name").Value, f.Element("Text").Value, 
                //                                      0                                         1                                   
                f.Element("Left").Value, f.Element("Top").Value, f.Element("Width").Value, f.Element("Height").Value,
                //                 2                                  3                                     4                                        5
                f.Element("FontName").Value, f.Element("FontSize").Value,  f.Element("FontItalic").Value=="true"?"italic":"normal",f.Element("FontBold").Value=="true"?"bold":"normal",
                //                6                                        7                                     8                                                                              9
                f.Element("ForeColor").Value ,f.Element("BackColor").Value};
            //                   10                                        11            
        }

        public string getWeb_label(XElement f)
        {
            getParam(f);
            var labletpl = string.Format(label, ob);
            var align = "left";
            if (f.Element("TextAlign") != null)
                align = f.Element("TextAlign").Value;
            labletpl = labletpl.Replace("[align]", align);
            return labletpl;
        }

        public String getWeb_table(XElement f)
        {
            getParam(f);
            String res = String.Format(table, ob);
            String operType = f.Element("GridView").Element("OperType").Value;
            if (operType == "手动填写")
            {
                var titles = f.Element("GridView").Element("Columns").Elements("ColTitle").ToArray();
                var formType = f.Element("GridView").Element("Rows").Attribute("FormType").Value;
                var rows = f.Element("GridView").Element("Rows").Elements("Row").ToArray();
                res = res.Replace("tableBanding", this.produTableByData(titles, rows, formType));
                res = res.Replace("sqlBanding", String.Empty);
            }
            return res;
        }

        private String produTableByData(XElement[] titles, XElement[] rows, String formType)
        {


            TableHelper tableHelper = new TableHelper();
            String table = String.Empty;
            var titCount = titles.Count();
            table += tableHelper.TableBegin("1500");
            table += tableHelper.BodyBegin;
            table += tableHelper.TbTrBegin("tbTitle");
            if (formType != "Approve")
            {
                table += "<th><a onclick='checkAllSala(this)' name='false'>全选</a></th>";
                for (int titIndex = 0; titIndex < titCount; titIndex++)
                {
                    table += tableHelper.TbTitle(titles[titIndex].Value, titles[titIndex].Attribute("ColName").Value);
                }
                table += "<th></th>";
            }
            else
            {
                for (int titIndex = 0; titIndex < titCount; titIndex++)
                {
                    table += tableHelper.TbTitle(titles[titIndex].Value, titles[titIndex].Attribute("ColName").Value);
                }
            }
            table += tableHelper.TrEnd;

            Int32 rowCount;
            String[] sqlRows = new String[1];
            rowCount = rows.Count();

            var width = 1500 / (titCount + 5);

            for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
            {
                var rowValue = rows[rowIndex].Value.Split(';');
                var userid = rows[rowIndex].Attribute("UserId").Value;
                var colCount = rowValue.Count() - 1;
                table += tableHelper.TbTrBegin((rowIndex + 1));
                if (formType == "Set")
                {
                    var selectRow = "1;3";
                    var moneyRow = "4;5;6;7";
                    String[] num = selectRow.Split(new Char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                    String[] mon = moneyRow.Split(new Char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

                    table += tableHelper.TbRowCheck(userid);
                    for (int colIndex = 0; colIndex < colCount; colIndex++)
                    {
                        if (num.Contains(colIndex.ToString()))
                        {
                            table += tableHelper.TbSelectRow(width.ToString(), colIndex.ToString());
                            //table += "<td><div class='drop_self' style='width:" + width.ToString() + "px' name='" + colIndex.ToString() + "'></div></td>";
                            continue;
                        }

                        if (colIndex == 9)
                        {
                            table += "<td><select class='earnExtern' onchange='setvalue(this)'><option></option><option>有</option><option>无</option></select><input type='hidden' class='tbvalget' value='' /></td>";
                            continue;
                        }

                        var fillable = titles[colIndex].Attribute("Fillable").Value;
                        if (fillable.ToLower() == "true")
                        {
                            if (mon.Contains(colIndex.ToString()))
                                table += tableHelper.TbTextRowForSala(rowValue[colIndex], width);
                            else
                                table += tableHelper.TbTextRow(rowValue[colIndex], width);
                        }
                        else
                            table += tableHelper.TbLableRow(rowValue[colIndex]);
                    }
                    table += tableHelper.TbButtonRow("保存", "savesalary(this,&apos;" + rowIndex.ToString() + "&apos;)");
                }
                else if (formType == "Start")
                {
                    for (int colIndex = 0; colIndex < colCount; colIndex++)
                    {
                        if (colIndex == 9 || colIndex == 10)
                        {
                            table += tableHelper.TbTextRow(rowValue[colIndex], width);
                            continue;
                        }
                        table += tableHelper.TbLableRow(rowValue[colIndex]);
                    }
                }
                else
                {
                    for (int colIndex = 0; colIndex < colCount; colIndex++)
                    {
                        table += tableHelper.TbLableRow(rowValue[colIndex]);
                    }
                }
                table += tableHelper.TbTrEnd();
            }
            table += tableHelper.BodyEnd;
            table += tableHelper.TableEnd;
            return table;

        }


    }

    public class TableHelper
    {
        public String TableEnd = "</table>";
        public String BodyBegin = "<tbody>";
        public String BodyEnd = "</tbody>";
        public String TrBegin = "<tr align='center'>";
        public String TrEnd = "</tr>";
        public String ThBegin = "<th>";
        public String ThEnd = "</th>";
        public String TdBegin = "<td>";
        public String TdEnd = "</td>";

        public String TableBegin()
        {
            return TableBegin(null);
        }

        public String TableBegin(String width)
        {
            width = width ?? "700";
            var table = "<table class='bdtable' width='" + width + "' cellspacing='0' cellpadding='0' border='1' style='table-layout:fixed; word-break:break-all;'>";
            return table;
        }

        public String TbTextRow(String rowValue, Int32 width)
        {
            var row = "<td><input align='center' type='text' style='width:" + width + "px;' class='txtunderline' value='" + rowValue
                + "' onblur='bandhidden(this)' onfocus='limitlength(this)' />" + this.rowHidden(rowValue) + "</td>";
            return row;
        }

        public String TbTextRowForSala(String rowValue, Int32 width)
        {
            var row = "<td><input align='center' type='text' style='width:" + width + "px;' class='txtunderline' value='" + rowValue
                + "' onblur='sumSalary(this)' />" + this.rowHidden(rowValue) + "</td>";
            return row;
        }

        public String TbButtonRow(String name, String even)
        {
            var row = "<td><input type='button' value='" + name + "' onclick='" + even + "' /></td>";
            return row;
        }

        public String TbTitle(String caption, String colName)
        {
            var col = "<th align='center'><lable>" + caption + "</lable>" + this.rowHidden(colName + "-" + caption) + "</th>";
            return col;
        }

        public String TbTitleCheck(String even)
        {
            var col = "<th><input type='checkbox' onclick='" + even + "' /></th>";
            return col;
        }

        public String TbRowCheck(String value)
        {
            var row = "<td><input type='checkbox'/>" + this.rowHidden(value) + "</td>";
            return row;
        }

        public String TbLableRow(String rowValue)
        {
            var row = "<td align='center'><lable>" + rowValue + "</lable>" + this.rowHidden(rowValue) + "</td>";
            return row;
        }

        public String TbSelectRow(String width, String cls)
        {
            var row = "<td align='center'><select class='" + cls + "' onchange='setvalue(this)'></select>" + this.rowHidden("") + "</td>";
            return row;
        }

        public String TbDivRow(String rowValue, String width)
        {
            var row = "<td align = 'center'><div class='drop_self' style='width:" + width + "px;'>" + rowValue + "</div>" + this.rowHidden(rowValue) + "</td>";
            return row;
        }

        public String TbHyPerLink(String text, String @event)
        {
            var hyper = "<a onclick='" + @event + "' style='color: black; text-underline-position: below; cursor: pointer'>" + text + "</a>";
            return hyper;
        }

        public String TbTrBegin(Int32 rowIndex)
        {
            return "<tr id='tbRow" + rowIndex + "'>";
        }

        public String TbTrBegin(String id)
        {
            return "<tr id='" + id + "'>";
        }

        public String TbTrEnd()
        {
            return "</tr>";
        }

        private String rowHidden(String value)
        {
            return "<input type='hidden' class='tbvalget' value='" + value + "' />";
        }

    }
}


